//
//  MBManager.m
//  MBProgressDemo
//
//  Created by ZhangYunguang on 16/1/23.
//  Copyright © 2016年 ZhangYunguang. All rights reserved.
//

#import "MBManager.h"

#define kScreen_height  [[UIScreen mainScreen] bounds].size.height
#define kScreen_width   [[UIScreen mainScreen] bounds].size.width

@interface MBManager ()<UIGestureRecognizerDelegate>

@property (nonatomic, copy) void(^clickBlock)();

@property (nonatomic, strong) MBProgressHUD *hud;
@property (nonatomic, strong) UITapGestureRecognizer *tap;

@end

@implementation MBManager


+ (instancetype)sharedManager{
    static MBManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (manager == nil) {
            manager = [[MBManager alloc] init];
            manager.tap = [[UITapGestureRecognizer alloc] initWithTarget:manager action:@selector(tapAction)];
            manager.hud = [[MBProgressHUD alloc] initWithView:[UIApplication sharedApplication].keyWindow];
        }
    });
    return manager;
}

+ (MBManager *)show{
    MBManager *manager = [self sharedManager];
    if (manager.hud.superview == nil) {
        manager.hud = [MBProgressHUD showHUDAddedTo:[UIApplication sharedApplication].keyWindow animated:YES];
        [manager.hud addGestureRecognizer:manager.tap];
    }
    return manager;
}

+ (void)showLoadingAlert:(NSString *)alert clickHidden:(BOOL)clickHidden clickBlock:(void (^)())block{
    MBManager *manager = [self show];
    
    manager.clickBlock = clickHidden== YES ? block : nil;
    manager.hud.label.text = [alert isNotEmpty] ? alert : ASLocalizedString(@"等待中");

}

+ (void)showBriefAlert:(NSString *)alert clickHidden:(BOOL)clickHidden clickBlock:(void (^)())block{
    MBManager *manager = [self show];
    
    manager.hud.mode = MBProgressHUDModeCustomView;
    
//    manager.hud.customView = [[UIImageView alloc] initWithImage:image];
    // Looks a bit nicer if we make it square.
    manager.hud.square = NO;
    // Optional label text.
    manager.hud.label.text = alert;
    manager.clickBlock = clickHidden== YES ? block : nil;

    [manager.hud hideAnimated:YES afterDelay:kShowTime];
}

+ (void)showPermanentAlert:(NSString *)alert clickHidden:(BOOL)clickHidden clickBlock:(void (^)())block{
    MBManager *manager = [self show];
    
    manager.hud.square = NO;
    manager.hud.label.text = alert;
    manager.clickBlock = clickHidden== YES ? block : nil;
//    [self show];
}

+ (void)showCustomView:(UIView *)customView title:(NSString *)title clickHidden:(BOOL)clickHidden clickBlock:(void (^)())block{
    MBManager *manager = [self show];

    manager.hud.mode = MBProgressHUDModeCustomView;
    manager.hud.customView = customView;
    manager.hud.label.text = title;
    manager.hud.label.textColor = [UIColor whiteColor];
    manager.hud.label.font = [UIFont systemFontOfSize:13];
    manager.hud.square = NO;
    [manager.hud setValue:@(MBProgressHUDBackgroundStyleSolidColor) forKeyPath:@"bezelView.style"];
    [manager.hud setValue:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.9] forKeyPath:@"bezelView.color"];
    manager.hud.offset = CGPointMake(0, kBDScreenHeight / 3.0 - kBDScreenHeight / 2.0);
    [manager.hud hideAnimated:YES afterDelay:kShowTime];
//    [self show];
}

+ (void)hideAlert{
    MBManager *manager = [self show];
    [manager.hud hideAnimated:YES];
}

- (void)tapAction{
    [self.hud hideAnimated:YES];
    if (self.clickBlock != nil) {
        self.clickBlock();
    }
}

@end
